package vn.com.absoft.android.musicclock;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;

public class UserSettingActivity extends Activity {

  private SharedPreferences preferences;
  private Editor edit;

  private int[] checkBoxIds = new int[24];

  /**
   * Called when the activity is first created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainusersetting);

    initCheckBoxIds();

    preferences = PreferenceManager.getDefaultSharedPreferences(this);
    edit = preferences.edit();

    for (int i = 0; i <= 23; i++) {
      CheckBox chkHour = (CheckBox) findViewById(checkBoxIds[i]);
      chkHour.setOnClickListener(new CheckBoxListener(i));
    }
  }

  /**
   * Set values of checkBoxIds array.
   */
  private void initCheckBoxIds() {
    checkBoxIds[0] = R.id.chk0hour;
    checkBoxIds[1] = R.id.chk1hour;
    checkBoxIds[2] = R.id.chk2hour;
    checkBoxIds[3] = R.id.chk3hour;
    checkBoxIds[4] = R.id.chk4hour;
    checkBoxIds[5] = R.id.chk5hour;
    checkBoxIds[6] = R.id.chk6hour;
    checkBoxIds[7] = R.id.chk7hour;
    checkBoxIds[8] = R.id.chk8hour;
    checkBoxIds[9] = R.id.chk9hour;
    checkBoxIds[10] = R.id.chk10hour;
    checkBoxIds[11] = R.id.chk11hour;
    checkBoxIds[12] = R.id.chk12hour;
    checkBoxIds[13] = R.id.chk13hour;
    checkBoxIds[14] = R.id.chk14hour;
    checkBoxIds[15] = R.id.chk15hour;
    checkBoxIds[16] = R.id.chk16hour;
    checkBoxIds[17] = R.id.chk17hour;
    checkBoxIds[18] = R.id.chk18hour;
    checkBoxIds[19] = R.id.chk19hour;
    checkBoxIds[20] = R.id.chk20hour;
    checkBoxIds[21] = R.id.chk21hour;
    checkBoxIds[22] = R.id.chk22hour;
    checkBoxIds[23] = R.id.chk23hour;
  }

  /**
   * Manipulate click on checkbox.
   */
  class CheckBoxListener implements OnClickListener {

    /**
     * Hour that this checkbox corresponds.
     */
    private int hour;

    public CheckBoxListener(int hour) {
      this.hour = hour;
    }

    public void onClick(View v) {
      CheckBox chkHour = (CheckBox) v;
      edit.putBoolean("hour_" + hour, chkHour.isChecked());
      edit.commit();
      boolean checked = preferences.getBoolean("hour_" + hour,
          chkHour.isChecked());
      String str = "Change Preferences - Input " + hour + " hour : " + checked;
      Log.d("Hour", str);
    }
  }
}
